package com.fastloan.mgt.dictionary.service.impl;

import com.fastloan.common.BasicServiceImpl;
import com.fastloan.mgt.dictionary.dao.DictionaryMapper;
import com.fastloan.mgt.dictionary.model.Dictionary;
import com.fastloan.mgt.dictionary.model.VO.DictionaryVO;
import com.fastloan.mgt.dictionary.service.DictionaryService;
import com.kawins.supers.tree.Bean2TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author admins
 * createTime 2017/11/06
 */
@Service
public class DictionaryServiceImpl extends BasicServiceImpl<Dictionary> implements DictionaryService {

    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Override
    public List<DictionaryVO> queryDictionaryVO(String groupName) {
        Dictionary dictionary = new Dictionary();
        dictionary.setGroupName(groupName);
        List<DictionaryVO> dictionaryVOList = new ArrayList<>();
        select(dictionary)
                .forEach(dictionary1 -> {
                    Bean2TreeUtil<DictionaryVO> treeUtil = new Bean2TreeUtil<>();
                    List<DictionaryVO> nodesByParentId
                            = treeUtil.getChildNodesByParentId(dictionaryMapper.queryDictionary(dictionary1.getId()), 0);
                    dictionaryVOList.addAll(nodesByParentId);
                });
        return dictionaryVOList;
    }

    @Override
    public Map<String, List<Dictionary>> queryDictionary(String groupName) {
        Map<String, List<Dictionary>> map = new HashMap<>();
        Dictionary dictionary = new Dictionary();
        dictionary.setGroupName(groupName);
        select(dictionary).forEach(action -> {
            Dictionary dictionaryEntity = new Dictionary();
            dictionaryEntity.setParentId(action.getId());
            map.put(action.getNameUs(), select(dictionaryEntity));
        });
        return map;
    }

}
